package de.app.fbc.user

import com.google.appengine.api.datastore.*
class RolleController {

	def persistenceManager
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
		def query = persistenceManager.newQuery( Rolle )
		def  rolleInstanceList = query.execute()
		def total = 0
		if(  rolleInstanceList &&  rolleInstanceList.size() > 0){
			total =  rolleInstanceList.size()
		}
		[  rolleInstanceList :  rolleInstanceList,  rolleInstanceTotal: total ]
    }

    def show = {
	    def rolleInstance = persistenceManager.getObjectById( Rolle.class, Long.parseLong( params.id )  )
        if(!rolleInstance) {
            flash.message = "Rolle not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ rolleInstance : rolleInstance ] }
    }

    def delete = {
	    def rolleInstance = persistenceManager.getObjectById( Rolle.class, Long.parseLong( params.id )  )
        if(rolleInstance) {
            try {
                persistenceManager.deletePersistent(rolleInstance)
                flash.message = "Rolle ${params.id} deleted"
                redirect(action:list)
            }
            catch(Exception e) {
                flash.message = "Rolle ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Rolle not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
	    def rolleInstance = persistenceManager.getObjectById( Rolle.class, Long.parseLong( params.id )  )
		if(!rolleInstance) {
            flash.message = "Rolle not found with id ${params.id}"
            redirect(action:list)
        }
        else {
			rolleInstance = persistenceManager.detachCopy( rolleInstance )    
        	return [ rolleInstance : rolleInstance ]
        }
    }

    def update = {
	 	def rolleInstance = persistenceManager.getObjectById( Rolle.class, Long.parseLong( params.id )  )
    
    	if(rolleInstance) {
            rolleInstance.properties = params
            if(!rolleInstance.hasErrors()){
	
				try{
					persistenceManager.makePersistent(rolleInstance)
				} catch( Exception e ){
				   	render(view:'edit',model:[rolleInstance:rolleInstance])
				}finally{
					flash.message = "Rolle ${params.id} updated"
	                redirect(action:show,id:rolleInstance.id)
				}        
 			}
            else {
                render(view:'edit',model:[rolleInstance:rolleInstance])
            }
        }
        else {
            flash.message = "Rolle not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def rolleInstance = new Rolle()
        rolleInstance.properties = params
        return ['rolleInstance':rolleInstance]
    }

    def save = {
        def rolleInstance = new Rolle(params)
		if(!rolleInstance.hasErrors() ) {
			try{
				persistenceManager.makePersistent(rolleInstance)
			} finally{
				flash.message = "Rolle ${rolleInstance.id} created"
				redirect(action:show,id:rolleInstance.id)	
			}
		}
   
		render(view:'create',model:[rolleInstance:rolleInstance])
        
    }
}
